package com.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.oa.pojo.Leave;
import com.oa.pojo.PageBean;
import com.oa.pojo.User;
import com.oa.service.LeaveService;
import com.oa.service.UserService;
import com.oa.utils.CommonUtil;
import com.oa.utils.MyWebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author Str
 * @Date 2021/12/7 15:02
 * @Description :
 */
@Controller
@RequestMapping("/leave")
public class LeaveController extends BaseController {
    @Autowired
    private LeaveService leaveService;

    @Autowired
    private UserService userService;

    // 跳转首页
    @RequestMapping("/login_temp")
    public String login_temp(@RequestParam("username") String username, HttpSession session) {
        // 根据用户名查找用户
        User user = userService.getOneByName(username);
        System.out.println(user);
        // 设置会话域
        session.setAttribute("user", user);
        // 内部转发
        return "index_temp";
    }

    // ---------------------------用户申请请假-------------------------------------
    // 展示用户流程申请页
    @RequestMapping("/showLeaveForm")
    public String showLeaveForm() {
        return "leave/leaveForm";
    }

    // 展示用户流程申请页
    @RequestMapping(value = "/submitLeave", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String submitLeave(@RequestParam Map map) {
        for (Object o : map.keySet()) {
            System.out.println(o + ":" + map.get(o));
        }
        // 这样做主要是为了转换日期类型，不然可以直接用Leave做参数
        Leave leave = new Leave();
        leave.setInitiator((String) map.get("initiator"));
        leave.setStartDate(CommonUtil.shortStringToDate((String) map.get("startDate")));
        leave.setEndDate(CommonUtil.shortStringToDate((String) map.get("endDate")));
        leave.setLeaveType((String) map.get("leaveType"));
        leave.setReason((String) map.get("reason"));
        String respTxt = "";
        JSONObject jo = new JSONObject();
        try {
            System.out.println("Leave::" + leave);
            // 发起申请
            leaveService.startProcess(leave);
            jo.put("result", "success");
            jo.put("leave", leave);
            respTxt = jo.toString();
        } catch (ParseException e) {
            // 封装错误信息
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }
    // ---------------------------用户申请请假-------------------------------------


    // ---------------------------用户查看自己发起的请假-------------------------------------
    // 跳转本用户发起的流程
    @RequestMapping("/myInitiate")
    public String myInitiate() {
        return "leave/myInitiate";
    }

    // 传递本用户发起流程的数据
    // 要做分页还要加页码数以及每页数据量
    @RequestMapping(value = "/jsonMyInitiate", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String jsonMyInitiate(@RequestParam("initiator") String initiator) {
        String respTxt = "";
        try {
            // 查询数据
            PageBean<Leave> leavePageBean = leaveService.queryMyInitiate(initiator);
            respTxt = packJSON(leavePageBean);
        } catch (Exception e) {
            // 封装错误信息
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }
    // ---------------------------用户查看自己发起的请假-------------------------------------


    // ---------------------------用户查看待我审批的流程-------------------------------------
    // 跳转
    @RequestMapping("/waitMyApprove")
    public String waitMyApprove() {
        return "leave/waitMyApprove";
    }

    @RequestMapping(value = "/jsonWaitMyApprove", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String jsonWaitMyApprove(
            @RequestParam("assigneeId") String assigneeId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {
        String respTxt = "";
        try {
            // 查询数据
            PageBean<Leave> leavePageBean = leaveService.queryWaitMyApprove(assigneeId, page, limit);
            respTxt = packJSON(leavePageBean);
        } catch (Exception e) {
            // 封装错误信息
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

    //{B.3}查询我的任务详情
    @RequestMapping(value = "/taskDetail", produces = "text/html;charset=UTF-8")
    public String taskDetail(@RequestParam("taskId") String taskId, Model model) {
        Leave leave = null;
        try {
            //{1}查询任务详情.
            leave = leaveService.queryMyTaskByTaskId(taskId);
            // 日期转换
            String startDate = CommonUtil.dateToShortString(leave.getStartDate());
            String endDate = CommonUtil.dateToShortString(leave.getEndDate());
            //{2}设置到模型域.
            model.addAttribute("leave", leave);
            model.addAttribute("startDate", startDate);
            model.addAttribute("endDate", endDate);
            //{3}内部转发到: taskDetail.jsp
            return "leave/taskDetail";
        } catch (Exception e) {
            e.printStackTrace();
            // 设置错误信息
            model.addAttribute("msg", setErrorMsg(e));
            //{4}这里暂时不去处理。
            return "error";
        }
    }
    // ---------------------------用户查看待我审批的流程-------------------------------------


    // ---------------------------用户提交任务审批-------------------------------------
    // 处理任务
    @RequestMapping(value = "/doApprove", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String doApprove(@RequestParam Map map) {
        String respTxt = "";
        JSONObject jo = new JSONObject();
        try {
            leaveService.completeMyApprove(map);
            jo.put("result", "success");
            jo.put("operation", "doApprove");
            respTxt = jo.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            // 设置错误信息
            //{4}这里暂时不去处理。
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }


    // ---------------------------用户查看自己已审核的请假-------------------------------------
    // 跳转本用户发起的流程
    @RequestMapping("/myReviewed")
    public String myReviewed() {
        return "leave/myReviewed";
    }

    // 传递本用户发起流程的数据
    // 要做分页还要加页码数以及每页数据量
    @RequestMapping(value = "/jsonMyReviewed", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String jsonMyReviewed(@RequestParam("assignee") String assignee) {
        String respTxt = "";
        try {
            // 查询数据
            PageBean<Leave> leavePageBean = leaveService.queryMyReviewed(assignee);
            respTxt = packJSON(leavePageBean);
        } catch (Exception e) {
            // 封装错误信息
            respTxt = setErrorMsg(e);
        }
        System.out.println("jsonMyReviewed"+respTxt);
        return respTxt;
    }
    // ---------------------------用户查看自己发起的请假-------------------------------------

}
